package com.brainsitter;

import java.util.ArrayList;

import com.brainsitter.db.DBHandler;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class RedeemTabActivity extends Activity{
	
	private ArrayList<Reward> rewardList;
	private Reward a, b, c, d, e, f;
	
	Spinner name;
	EditText description;
	TextView cost;
	DBHandler db;
	TextView earned;
	Button redeem;
	Reward temp;

	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.redeem);
        db = new DBHandler(this);
        initializeValues();
        initializeRewards();
        initializeContents();
	}
	
	public void initializeValues() {
		name = (Spinner) findViewById(R.id.reward);
		description = (EditText) findViewById(R.id.DESC);
		cost = (TextView) findViewById(R.id.cost);
		earned = (TextView) findViewById(R.id.points);
		redeem = (Button) findViewById(R.id.RedeemButton);
	}
	
	public void initializeRewards() {		
		rewardList = new ArrayList<Reward>();
		a = new Reward("BB SOCIAL", "30-day subscription of BBSocial brought to you by Globe and Blackberry.", 299);
		b = new Reward("BB MAX", "30-day subscription of BBMax brought to you by Globe and Blackberry.", 599);
		c = new Reward("UNLITXT20", "One day of unlimited Globe to Globe texts.", 200);
		d = new Reward("McDonald's McSavers Meal", "One e-coupon for one McSavers Meal.", 400);
		e = new Reward("Can of Coke", "One e-coupon for a can of any Coca-Cola Product.", 200);
		f = new Reward("Can of SMB", "One e-coupon for a can of San Miguel Beer.", 300);
		rewardList.add(a); rewardList.add(b); rewardList.add(c); rewardList.add(d); rewardList.add(e);rewardList.add(f);
	}
	
	public void initializeContents() {
		String[] array_spinner = new String[6];
		for(int i = 0; i<6; i++)
			array_spinner[i] = rewardList.get(i).getName();
		ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, array_spinner);
		name.setAdapter(adapter);
		name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
        public void onItemSelected(AdapterView parent,View v, int i, long l)
            {
                temp = rewardList.get(i);
                description.setText(temp.getDescription());
                cost.setText(String.valueOf(temp.getCost()));
            }

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
         });
		
		earned.setText(String.valueOf(db.getPoints()));
		redeem.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				if(db.getPoints() < temp.getCost())
				{
					Context context = getApplicationContext();
					CharSequence text = "Not enough points.";
					int duration = Toast.LENGTH_SHORT;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				}
				redeemReward(temp.getCost());
			}
		});
	}
	private void redeemReward(int cost) {
		// TODO Auto-generated method stub

		int result = db.getPoints() - cost;
		db.updatePoints(result);
	}

	public void onResume(){
		super.onResume();
        initializeValues();
        initializeRewards();
        initializeContents();
	}

}